package com.adwin.addcoin.ui.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.adwin.addcoin.R
import com.adwin.addcoin.databinding.HomeTaskItemBinding
import com.adwin.addcoin.repository.bean.HomeTaskBean
import com.adwin.addcoin.ui.task.TaskDetailActivity
import com.adwin.addcoin.ui.viewholder.CheckInHolder
import com.adwin.addcoin.ui.viewholder.TaskItemHolder
import com.adwin.addcoin.utils.installRepost
import com.bumptech.glide.Glide
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.json.JSONObject

class TaskItemAdapter(val tasks:List<HomeTaskBean>, val mContext:Context) : Adapter<TaskItemHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskItemHolder {
        val binding = HomeTaskItemBinding.inflate(LayoutInflater.from(mContext))

        return TaskItemHolder(binding)
    }

    override fun getItemCount(): Int = tasks.size

    override fun onBindViewHolder(holder: TaskItemHolder, position: Int) {
        val bean = tasks[position]
        holder.upDateTask(bean)
        holder.itemView.setOnClickListener {
            MainScope().launch(Dispatchers.IO) {
                val obj = JSONObject()
                obj.put("taskid",bean.orderId)
                obj.put("taskName",bean.appName)
                mContext.installRepost("clickTask",obj)
            }
            TaskDetailActivity.start(mContext,bean.orderId,-1,bean.appName?:"",bean.description?:"",bean.taskIcon?:"")
        }
    }
}